﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Diagnostics;

namespace MSAS.DIP.Common
{
    /// <summary>
    /// Class to store the ConfigurationReport for a newly instantiated
    /// Dip Component.
    /// </summary>
    public class ConfigurationReport
    {
        #region Fields

        /// <summary>
        /// Mapping of properties to values
        /// </summary>
        private readonly Dictionary<string, object> m_configuration = new Dictionary<string, object>();

        #endregion

        #region Methods

        /// <summary>
        /// Add a configuration property and its value to the report
        /// </summary>
        public void Add(string key, object value)
        {
            m_configuration.Add(key, value);
        }

        #endregion

        #region Overridden Methods

        /// <summary>
        /// Write the configuration report to a string
        /// </summary>
        public override string ToString()
        {
            //
            // Print the configuration.  ToString() will be called recursively to handle
            // class inheritance.  We are indenting these recursive calls.
            //

            // Get the number of recursive calls to ConfigurationReport.ToString minus this call
            int tabs = new StackTrace().GetFrames().Count(frame => frame.GetMethod().Name == "ToString" && frame.GetMethod().DeclaringType == typeof(ConfigurationReport)) - 1;

            // Build the configuration string
            StringBuilder configurationOutput = new StringBuilder();
            foreach (KeyValuePair<string, object> entry in m_configuration)
            {
                configurationOutput.Append(Environment.NewLine);
                for (int i = 0; i < tabs; i++)
                {
                    configurationOutput.Append("\t");
                }
                configurationOutput.AppendFormat("{0} = {1}", entry.Key, entry.Value.ToString());
            }
            return configurationOutput.ToString();
        }

        #endregion
    }
}
